using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

//UI基类
public class UIBase : MonoBehaviour
{
    public Dictionary<string, Transform> tranDict = new Dictionary<string, Transform>();
    public Dictionary<string, Image> imgDict = new Dictionary<string, Image>();
    public Dictionary<string, Button> btnDict = new Dictionary<string, Button>();
    public Dictionary<string, Toggle> togDict = new Dictionary<string, Toggle>();
    public Dictionary<string, Slider> sldDict = new Dictionary<string, Slider>();

    public virtual void Init()
    {
        RecursionGetAllNode(transform);
        foreach (var str in tranDict.Keys)
        {
            if(str.Substring(0,4) == "img_")
                imgDict.Add(str, tranDict[str].GetComponent<Image>());
            else if(str.Substring(0,4) == "btn_")
                btnDict.Add(str, tranDict[str].GetComponent<Button>());
            else if(str.Substring(0,4) == "tog_")
                togDict.Add(str, tranDict[str].GetComponent<Toggle>());
            else if(str.Substring(0,4) == "sld_")
                sldDict.Add(str, tranDict[str].GetComponent<Slider>());
        }
    }


    private void RecursionGetAllNode(Transform rootTran)
    {
        foreach (Transform childTran in rootTran)
        {
            if (tranDict.ContainsKey(childTran.name))
            {
                Debug.Log("该名子的物体已存储 = " + childTran.name);
            }
            else
            {
                tranDict.Add(childTran.name, childTran);
                Debug.Log(childTran.name);
            }

            RecursionGetAllNode(childTran);
        }
    }
}
